// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu, ipcMain } = require("electron");

// 导入菜单
const { template } = require("./meau");

const path = require("node:path");

let mainWindow;
function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800, // 窗口的宽度
    height: 600, // 窗口的高度
    frame: false, // 窗口的默认菜单是否显示
    // transparent:true, //窗口是否透明
    icon: "./icon/1.jpg", //设置窗口的图标
    // title: "exe", // 窗口的标题，优先级低于加载文件
    // show: true, // 窗口是否显示
    // resizable: true, //窗口是否可以调整---窗口是否可以放大或者缩小
    // center: "true", // 窗口是否在屏幕居中
    // movable: true, // 窗口是否可以移动 默认为true
    // minimizable: true, // 窗口是否可以最小化 默认为true
    // maximinzabe: true, // 窗口是否可以最大化 默认为true
    // closable: true, // 指定窗口是否可以关闭，默认为true
    // fullscreenable: true, //指定窗口是否可以进入全屏模式，模式为true
    // alwaysOnTop: false, // 指定窗口始终在其他窗口之上
    // fullscreen: false, //指定窗口是否可以进入全屏模式，默认为true--会导致最小化/半屏/关闭隐藏按钮

    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
    },
  });

  // 创建菜单对象
  const menu = Menu.buildFromTemplate(template);
  // 设置应用程序菜单
  Menu.setApplicationMenu(menu);

  // and load the index.html of the app.

  // mainWindow.loadFile('index.html')
  global._static = path.join(__dirname, "/vue-dist").replace(/\\/g, "\\\\");
  // vue项目打包引入
  // mainWindow.loadURL(`${global._static}/index.html`)
  // vue项目网址引入
  mainWindow.loadURL("http://localhost:8080/");

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}
// // 最小化到系统栏
ipcMain.on("minimize", (event) => {
  mainWindow.minimize();
});
ipcMain.on("windowStatus", (event, val) => {
  if (mainWindow.isMaximized()) {
    mainWindow.unmaximize();
  } else {
    mainWindow.maximize();
  }
});
ipcMain.on("closeWin", (event) => {
  mainWindow.close();
});
ipcMain.on("drag-window", (event, { offsetX, offsetY }) => {
  const currentWindow = BrowserWindow.getFocusedWindow();
  // 获取当前窗口的位置
  const currentPosition = currentWindow.getPosition();
  const newX = currentPosition[0] + offsetX;
  const newY = currentPosition[1] + offsetY;
  // 设置窗口的新位置，将窗口移动到新的坐标位置
  currentWindow.setPosition(newX, newY);
  // const currentSize = currentWindow.getSize();
  // // 设置窗口的大小，保持不变
  // currentWindow.setSize(currentSize[0], currentSize[1]);
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow();

  app.on("activate", function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
